package com.suixinsanren.design.state.demo.demo06;

public class GumballMachine {

    private final State hasQuarterState = new HasQuarterState(this);
    private final State noQuarterState = new NoQuarterState(this);
    private final State soldState = new SoldState(this);
    private final State soldOutState = new SoldOutState(this);
    private final State winnerState = new WinnerState(this);

    private State state;
    private int count;

    public GumballMachine(int gumballNumber) {
        count = gumballNumber;
        state = count > 0 ? noQuarterState : soldOutState;
    }

    public void insertQuarter() {
        state.insertQuarter();
    }

    public void ejectQuarter() {
        state.ejectQuarter();
    }

    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    public void refill(int numberGumball) {
        state.refill(numberGumball);
    }

    public void releaseGumball() {
        if (count > 0) {
            count--;
            System.out.println("您获得了1颗糖果");
        }
    }

    @Override
    public String toString() {
        return "糖果机{" +
                "当前状态=" + state +
                ", 当前糖果数量=" + count +
                '}';
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getWinnerState() {
        return winnerState;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
